//
//  KWTCustomMarkerView.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/8/15.
//

#import "KWTCustomMarkerView.h"
#import "DeviceChartFieldModel.h"

@interface KWTCustomMarkerView()

@property (nonatomic ,strong) ChartDataEntry *entry;

@property (nonatomic, strong) ChartHighlight *highlight;

@property (nonatomic, strong) NSArray *colorArr;

@property (nonatomic, assign) CGSize contentSize;

@end
@implementation KWTCustomMarkerView

-(void)refreshContentWithEntry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight
{
    NSLog(@"---->entry:%@",entry.yy_modelDescription);
    NSLog(@"---->highlight:%@",highlight.yy_modelDescription);
    self.entry = entry;
    self.highlight = highlight;
    [self caculateContentSize:(int)entry.x];
    
    CGFloat offset_x = 0;
    CGFloat offset_y = 0;
    
    if((self.chartView.width - highlight.drawX)<self.contentSize.width){
        offset_x = self.chartView.width - highlight.drawX - self.contentSize.width;
    }
    if((self.chartView.height - highlight.drawY)<self.contentSize.height){
        offset_y = (self.chartView.height - highlight.drawY) - self.contentSize.height;
    }
    self.offset = CGPointMake(offset_x, offset_y);
    NSLog(@"-------->offset:%@",NSStringFromCGPoint(self.offset));
}

- (void)drawRect:(CGRect)rect{
    
//    CGPoint startPoint = CGPointMake(point.x+self.offset.x, point.y+self.offset.y);
    CGPoint startPoint = CGPointMake(0, 0);
    
//    NSLog(@"----->point:%@",NSStringFromCGPoint(point));
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(startPoint.x, startPoint.y, self.contentSize.width, self.contentSize.height) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(8, 8)];
    [kColorRGBA(0, 0, 0, 0.6) set];
    [path fill];
    
   __block CGFloat w = 0;
   __block CGFloat h = 5;
    
    NSString *timeStr = self.chartModel.xTitlesArray[(int)self.entry.x];
    CGFloat timeWidth = [timeStr getStringWidthFont:kSYSTEMFONT(14) limitHeight:20]+1;
    CGFloat timeHeight = [timeStr getStringHeightFont:kSYSTEMFONT(14) limitWidth:timeWidth]+1;
    [timeStr drawInRect:CGRectMake(startPoint.x+5, startPoint.y+5, timeWidth, timeHeight) withAttributes:@{NSForegroundColorAttributeName:UIColor.whiteColor}];
    h = h + timeHeight ;
    
    [_chartModel.selectArray enumerateObjectsUsingBlock:^(DeviceChartFieldModel  *_Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if(model.isShowLine){
            CGFloat content_w = 0;
            
            CGFloat text_height = [@"AB" getStringHeightFont:kSYSTEMFONT(12) limitWidth:300];
            CGFloat point_x = startPoint.x + 5;
            CGFloat point_y = startPoint.y + h + ((text_height - 6)/2);
            CGFloat point_w = 6;
            UIBezierPath *pointPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(point_x, point_y, point_w, point_w)];
            [kCOLOR_HEX(_colorArr[idx%_colorArr.count]) set];
            [pointPath fill];
            content_w = point_w + point_x;
            
            
            CGFloat keyStr_x = point_x + point_w + 5;
            NSString *keyStr = [NSString stringWithFormat:@"%@:",model.keyName.rh_localized];
            CGFloat keyStr_width = [keyStr getStringWidthFont:kSYSTEMFONT(12) limitHeight:20]+1;
            [keyStr drawInRect:CGRectMake(keyStr_x, startPoint.y+h, keyStr_width, text_height) withAttributes:@{NSForegroundColorAttributeName:UIColor.whiteColor}];
            content_w = keyStr_x + keyStr_width;
            
            
            CGFloat value_x = keyStr_x + keyStr_width;
            
            NSString *value = [NSDecimalNumber decimalStringWithDouble:[model.dataArray[(int)self.entry.x] doubleValue]];
            
            NSString *valueStr = [NSString stringWithFormat:@"%@%@",value,model.unitName];
            CGFloat valueStr_width = [valueStr getStringWidthFont:kSYSTEMFONT(12) limitHeight:20]+1;
            [valueStr drawInRect:CGRectMake(value_x, startPoint.y+h, valueStr_width, text_height) withAttributes:@{NSForegroundColorAttributeName:UIColor.whiteColor}];
            h += text_height;
            content_w = value_x + valueStr_width + 5;
            w = MAX(w, content_w);
            
            h += 5;
        }
       
        
    }];
}

-(void)caculateContentSize:(int)index
{
    __block CGFloat h = 5;
    __block CGFloat w = 5;
    QMUILabel *timeLab = [QMUILabel labelWithText:@"AB" font:kSYSTEMFONT(14) textColor:UIColor.whiteColor];
    [timeLab sizeToFit];
    
    h += timeLab.height + 1;
    w += timeLab.width +1;
    
    NSMutableArray *arr = [NSMutableArray array];
    [_chartModel.selectArray enumerateObjectsUsingBlock:^(DeviceChartFieldModel  *_Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if(model.isShowLine){
            CGFloat content_w = 5;
            //圆点
            content_w += 6;
            
            NSString *key = [NSString stringWithFormat:@"%@:",model.keyName.rh_localized];
            UILabel *keyLab = [QMUILabel labelWithText:key font:kSYSTEMFONT(12) textColor:UIColor.whiteColor];
            [keyLab sizeToFit];
            content_w = content_w + 5 + keyLab.width+1;
            
            NSString *value = [NSDecimalNumber decimalStringWithDouble:[model.dataArray[index] doubleValue]];
            NSString *valueStr = [NSString stringWithFormat:@"%@%@",value,model.unitName];
            UILabel *contentLab = [QMUILabel labelWithText:valueStr font:kSYSTEMFONT(12) textColor:UIColor.whiteColor];
            [contentLab sizeToFit];
            
            content_w = content_w + contentLab.width+1 + 5;
            
            w = MAX(w, content_w);
            h = h + keyLab.height + 1;
            
            h += 5;
        }
        
        
    }];
    
    self.contentSize = CGSizeMake(w, h);
    NSLog(@"------>contentSize:%@",NSStringFromCGSize(self.contentSize));
}

-(void)updateModel:(DevChartModel*)chartModel  colors:(NSArray*)colorArr chartView:(UIView *)chartView entry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight;
{
    _chartModel = chartModel;
    _colorArr = colorArr;
    [chartView addSubview:self];
    [self refreshContentWithEntry:entry highlight:highlight];
    self.frame = CGRectMake(0, -100, self.contentSize.width, self.contentSize.height);
    [self setNeedsDisplay];
}

@end
